package com.skateboard.reelviewtest.loopviewpager

import android.util.Log
import kotlin.math.abs

/**
 * @作者： xuze
 * @时间： 2021/3/5 7:26 PM
 * @描述：
 */
class DataProvider2 {
    /**
     * viewpager 当前正在显示的页面在Adapter中对应的位置
     */
    companion object {
        var adapterPosition: Int = 100000 + 5
    }
    var isDeleteJishu = false
    /**
     * viewpager 当前正在显示的页面在datalist中对应的位置
     */
    private var dataIndex: Int = 0

    var dataList = mutableListOf<String>()

    /**
     *
     */
    init {
        dataList.addAll(createData())
        dataList.add(5, "8866")
        dataIndex = 5
    }

    private fun createData(): List<String> {
        var list = mutableListOf<String>()
        for (i in 0..9) {
            list.add("$i")
        }
        return list
    }

    fun getData(position: Int): String? {
        var str : String?= ""
        var index =dataIndex + position - adapterPosition
        if(index<0){
            index=dataList.size-1
        }
        if(index>=dataList.size){
            index=0
        }
        str = dataList[index]
        return str
    }



    var isAddHead= false
    var isAddFoot= false

    fun setCurrentAdapterPosition(position : Int){
        var offset= position - adapterPosition
        dataIndex+=offset
        if(dataIndex>=dataList.size){
            dataIndex -= dataList.size
        }
        if(dataIndex<0){
            dataIndex += dataList.size
        }

        adapterPosition=position

        if(!isAddHead&&dataIndex==1&&offset<0){
            isAddHead=true
            addHead()
        }
        if(!isAddFoot&&dataIndex==dataList.size-2&&offset>0){
            isAddFoot=true
            addFoot()
        }
    }


    private fun addHead(){
        for (i in 1..5) {
            dataList.add(0, "-$i")
            dataIndex++
        }
    }

    private fun addFoot(){
        for (i in 10..15) {
            dataList.add("$i")
        }
    }

     fun  deleteData(value : String,isNext:Boolean){
        var index = dataList.indexOf(value)
         when {
             index>dataIndex -> {
                 dataList.removeAt(index)
             }
             index<dataIndex -> {
                 dataList.removeAt(index)
                 dataIndex--
             }
             else -> {
                 dataList.removeAt(index)
                 if(isNext){
                     dataIndex++
                 }else{
                     dataIndex--
                 }
                 if(dataIndex>=dataList.size){
                     dataIndex -= dataList.size
                 }
                 if(dataIndex<0){
                     dataIndex += dataList.size
                 }

             }
         }


    }
}